//
//  HttpTools.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/11.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "HttpTools.h"

@implementation HttpTools


+ (AFHTTPSessionManager *)createManager
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    //将token封装入请求头
//    if (XJD_UserID) {
//        [manager.requestSerializer setValue:XJD_UserID forHTTPHeaderField:@"userId"];
//    }
//    [manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]] forHTTPHeaderField:@"Authorization"];
    
    manager.requestSerializer.timeoutInterval = 15.0f;
    return manager;
}



+ (void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1. 获取请求管理者
    
    if(![url containsString:@"http://"] && ![url containsString:@"https://"]){
        url = [NSString stringWithFormat:@"%@%@", urlHost, url];
    }else{
        url = url;
    }
//    NSMutableDictionary *paramsWithSign = [[NSMutableDictionary alloc] initWithDictionary:[params keysWithOther:[params encrypt]]];
    
    //    NSLog(@"传给后台的字典是>>>%@",paramsWithSign);
    // 2. 发送get请求
    [[self createManager] GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(success){
            success(responseObject);
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if(failure){
            if (error.code == -1001) {
                [SVProgressHUD dismiss];
                [SVProgressHUD showError:@"网络开小差,请稍后再试"];
            }
            failure(error);
            
        }
    }];
}

+ (void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1. 获取请求管理者
    if(![url containsString:@"http://"] && ![url containsString:@"https://"]){
        url = [NSString stringWithFormat:@"%@%@", urlHost, url];
    }else if([url containsString:@"http://"]){
        url = url;
    }
    // NSMutableDictionary *paramsWithSign = [[NSMutableDictionary alloc] initWithDictionary:[params keysWithOther:[params encrypt]]];
    //    NSLog(@"传给后台的字典是>>>%@",paramsWithSign);
    // 2. 发送post请求
    [[self createManager] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(success){
            success(responseObject);
            
            NSString *resultCode = [responseObject objectForKey:@"respCode"];
            if([resultCode isEqualToString:@"100106"]){
            }
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(failure){
            if (error.code == -1001) {
                [SVProgressHUD dismiss];
                [SVProgressHUD showError:@"网络开小差,请稍后再试"];
            }
            failure(error);
        }
    }];
}

+ (void)put:(NSString *)url params:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *))failure {
    // 1. 获取请求管理者
    if(![url containsString:@"http://"] || ![url containsString:@"https://"]){
        url = [NSString stringWithFormat:@"%@%@", urlHost, url];
    }else if([url containsString:@"http://"]){
        url = url;
    }
    NSMutableDictionary *paramsWithSign = [[NSMutableDictionary alloc] initWithDictionary:[params keysWithOther:[params encrypt]]];
    //    NSLog(@"传给后台的字典是>>>%@",paramsWithSign);
    // 2. 发送put请求
    [[self createManager] PUT:url parameters:paramsWithSign success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(success){
            success(responseObject);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(failure){
            
            failure(error);
        }
    }];
}

@end
